package com.alit.face.koala.controller;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alit.face.koala.bean.http.KoalaRequestData;
import com.alit.face.koala.entity.KoalaCemara;
import com.alit.face.koala.service.CallBackService;
import com.alit.face.koala.service.KoalaCemaraService;
import com.alit.face.koala.service.KoalaMqService;
import com.litong.utils.request.RequestUtil;

import lombok.extern.slf4j.Slf4j;

/**
 * @author litong
 * @date 2019年4月16日_下午12:41:16
 * @version 1.0
 */
@RestController
@RequestMapping({ "", "/", "/callback" })
@Slf4j
public class CallBackController {

  @Autowired
  private CallBackService koalaService;
  @Autowired
  private KoalaCemaraService kcs;
  @Autowired
  private KoalaMqService koalaMqService;

  @RequestMapping()
  public String index(HttpServletRequest request) {

    String remoteAddr = RequestUtil.getRemoteAddr(request);

    String requestString = null;
    try {
      requestString = RequestUtil.getRequestString(request);
    } catch (IOException e) {
      e.printStackTrace();
    }
    if (!StringUtils.isEmpty(requestString)) {
      KoalaRequestData koalaRequestData = JSON.parseObject(requestString, KoalaRequestData.class);
      KoalaCemara koalaCemara = kcs.findTopic(koalaRequestData.getScreenToken());
      String screenToken = koalaRequestData.getScreenToken();

      if (remoteAddr.equals("127.0.0.1")) { // 0:0:0:0:0:0:0:1
        koalaMqService.sendMessage(koalaCemara, "nginx配置有误,获取不到请求的IP地址");
        return "";
      }
      kcs.saveScreenToken(screenToken, koalaRequestData.getTimestamp()); // 更新识别时间
      long start = System.currentTimeMillis();
      log.info("\r\n");
      log.info("开始处理回调请求:" + start);
      
      koalaService.process(koalaRequestData, remoteAddr);// 处理回调请求
      long end = System.currentTimeMillis();
      log.info("回调请求处理完成:" + (end - start));
    }
    String retval = "回调地址验证成功";
    return retval;
  }

  /**
   * 打印出Request的头部
   * 
   * @param request
   */
  @SuppressWarnings("unused")
  private void printlnHeader(HttpServletRequest request) {
    Enumeration<String> headerNames = request.getHeaderNames();
    while (headerNames.hasMoreElements()) {
      String headerName = headerNames.nextElement();
      String headerValue = request.getHeader(headerName);
      System.out.println(headerName + ":" + headerValue);
    }
  }

  /**
   * 打印识别日志
   * 
   * @param request
   * @return
   */
  @SuppressWarnings("unused")
  private String logRequest(HttpServletRequest request) {
    String requestString = null;
    try {
      requestString = RequestUtil.getRequestString(request);
    } catch (IOException e) {
      e.printStackTrace();
    }
    log.info(requestString);
    return requestString;
  }
}
